<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdminUpload;
use App\Models\AdminClass;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use PhpOffice\PhpWord\IOFactory;
use Illuminate\Http\Request;

class AdminUploadController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdminUpload(), function (Grid $grid) {
            $grid->column('upload_id','ID')->sortable();
            $grid->column('class_one');
            $grid->column('class_two');

            $grid->column('upload_content');
            $grid->column('upload_title');
            $grid->column('upload_status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('upload_id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdminUpload(), function (Show $show) {
            $show->field('upload_id');
            $show->field('class_one');
            $show->field('class_two');
            $show->field('upload_content');
            $show->field('upload_title');
            $show->field('upload_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }
    public function city(Request $request)
    {
        $provinceId = $request->get('q');

        return \App\Models\AdminClass::all()->where('parent_id', $provinceId);
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminUpload(), function (Form $form) {
            $form->display('upload_id','ID');
            $form->select('class_one', '一级分类')->options(
                AdminClass::all()->where('parent_id',0)->pluck('class_name','class_id'))
                ->load('class_two', 'classList');

            $form->select('class_two','二级分类');
            $form->file('upload_title')->autoUpload()->accept('docx', '*/*');
//            $form->text('upload_status');

            $form->display('created_at');
            $form->display('updated_at');
            $form->uploaded(function (Form $form, UploadFieldInterface $field, UploadedFile $file, &$response){

                // 获取文件上传字段名称
                $response = (array) $response->getData();
                // 文件上传成功
                if ($response['status']) {
                    // 文件访问地址
                    $url = $response['url'];

                    $phpWord  = IOFactory::load($file);
                    $xmlWriter = IOFactory::createWriter($phpWord, "HTML");
                    $response = $xmlWriter->getContent();
                }
            });
            $form->editor('upload_content');
        });
    }
    public function classList(Request $request){
        $projectId = $request->get('q');
        return AdminClass::where('parent_id',$projectId)->get(['class_id as id', 'class_name as text']);
    }
}
